﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using supermarket.method;
using System.Data.SqlClient;

namespace supermarket
{
    public partial class Newstores : Form
    {
        public Newstores()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection();

        private void btnSave_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtShopCode.Text) || string.IsNullOrWhiteSpace(txtShopName.Text))
            {
                MessageBox.Show("店铺编号和名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!DateTime.TryParse(txtShopOpened.Text, out DateTime shopOpened))
            {
                MessageBox.Show("请输入有效的开业日期（格式：yyyy-MM-dd）！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                Classgetsql sqlcon = new Classgetsql();
                using (SqlConnection con = sqlcon.getcon())
                {
                    string checkQuery = "SELECT COUNT(*) FROM super.dbo.shop_List WHERE shop_code = @shopCode";
                    using (SqlCommand checkCmd = new SqlCommand(checkQuery, con))
                    {
                        checkCmd.Parameters.AddWithValue("@shopCode", txtShopCode.Text.Trim());
                        int count = (int)checkCmd.ExecuteScalar();
                        if (count > 0)
                        {
                            MessageBox.Show($"店铺编号 {txtShopCode.Text} 已存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    string insertQuery = @"
                        INSERT INTO super.dbo.shop_List (
                            shop_code, shop_name, shop_location, shop_manager, 
                            shop_phoneNum, shop_opened, shop_region
                        )
                        VALUES (
                            @shopCode, @shopName, @shopLocation, @shopManager, 
                            @shopPhoneNum, @shopOpened, @shopRegion
                        )";
                    using (SqlCommand cmd = new SqlCommand(insertQuery, con))
                    {
                        cmd.Parameters.AddWithValue("@shopCode", txtShopCode.Text.Trim());
                        cmd.Parameters.AddWithValue("@shopName", txtShopName.Text.Trim());
                        cmd.Parameters.AddWithValue("@shopLocation", txtShopLocation.Text.Trim());
                        cmd.Parameters.AddWithValue("@shopManager", txtShopManager.Text.Trim());
                        cmd.Parameters.AddWithValue("@shopPhoneNum", txtShopPhoneNum.Text.Trim());
                        cmd.Parameters.AddWithValue("@shopOpened", shopOpened.ToString("yyyy-MM-dd"));
                        cmd.Parameters.AddWithValue("@shopRegion", txtShopRegion.Text.Trim());

                        int rowsAffected = cmd.ExecuteNonQuery();
                        if (rowsAffected > 0)
                        {
                            string insertSalesQuery = @"
                                INSERT INTO super.dbo.shop_sales (shop_code, shop_name, month_date, sales)
                                VALUES (@shopCode, @shopName, @monthDate, 0)";
                            using (SqlCommand salesCmd = new SqlCommand(insertSalesQuery, con))
                            {
                                salesCmd.Parameters.AddWithValue("@shopCode", txtShopCode.Text.Trim());
                                salesCmd.Parameters.AddWithValue("@shopName", txtShopName.Text.Trim());
                                salesCmd.Parameters.AddWithValue("@monthDate", new DateTime(DateTime.Now.Year, 1, 1));
                                salesCmd.ExecuteNonQuery();
                            }
                            MessageBox.Show("新增门店成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //更新表
              
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("新增门店失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"新增门店时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
    }
}
